package com.accloud.cloudservice;

import com.accloud.cloudservice.ACSocketManager;
import com.accloud.service.ACException;
import com.accloud.service.Topic;
import com.accloud.utils.LogUtil;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/* access modifiers changed from: package-private */
public class ACAbsDataManager implements ACSocketManager.ConnectionListener {
    private static final String TAG = "ACAbsDataManager";
    private Set<Topic> mTopics = new HashSet();

    public ACAbsDataManager() {
        AC.sSockectManager.addConnectionListener(this);
    }

    public synchronized void subscribe(Topic topic, VoidCallback callback) {
        LogUtil.d(TAG, "subscribe: " + topic);
        this.mTopics.add(topic);
        AC.sSockectManager.tryConnect();
        AC.sSockectManager.subscribe(topic, callback);
    }

    public synchronized void unSubscribe(Topic topic, VoidCallback callback) {
        LogUtil.d(TAG, "unSubscribe: " + topic);
        boolean contains = this.mTopics.contains(topic);
        this.mTopics.remove(topic);
        if (contains) {
            AC.sSockectManager.unSubscribe(topic, callback);
        }
    }

    public synchronized void unSubscribeAll() {
        unSubscribeAllType(null);
    }

    /* access modifiers changed from: protected */
    public synchronized void unSubscribeAllType(String type) {
        LogUtil.d(TAG, "unSubscribeAllType " + type);
        HashSet<Topic> todoList = new HashSet<>();
        if (type != null) {
            for (Topic topic : this.mTopics) {
                if (type.equals(topic.getType())) {
                    todoList.add(topic);
                }
            }
            Iterator<Topic> it = todoList.iterator();
            while (it.hasNext()) {
                this.mTopics.remove(it.next());
            }
        } else {
            todoList.addAll(this.mTopics);
            this.mTopics.clear();
        }
        Iterator<Topic> it2 = todoList.iterator();
        while (it2.hasNext()) {
            Topic topic2 = it2.next();
            if (!AC.sSockectManager.isLogined()) {
                break;
            }
            AC.sSockectManager.unSubscribe(topic2, null);
        }
    }

    @Override // com.accloud.cloudservice.ACSocketManager.ConnectionListener
    public synchronized void onLogin() {
        for (Topic topic : this.mTopics) {
            AC.sSockectManager.subscribe(topic, new VoidCallback() {
                /* class com.accloud.cloudservice.ACAbsDataManager.AnonymousClass1 */

                @Override // com.accloud.cloudservice.VoidCallback
                public void success() {
                }

                @Override // com.accloud.cloudservice.BaseCallback
                public void error(ACException e) {
                }
            });
        }
    }
}
